package org.ccvi.policy.web.userinfo;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.ccvi.policy.entity.userinfo.Resource;
import org.ccvi.policy.entity.userinfo.User;
import org.ccvi.policy.service.userinfo.UserInfoManager;
import org.ccvi.policy.util.PolicyConstants;
import org.ccvi.policy.web.CrudActionSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.orm.hibernate.HibernateWebUtils;
import org.springside.modules.web.struts2.Struts2Utils;

import com.opensymphony.xwork2.ActionSupport;

@Results( {
	@Result(name = CrudActionSupport.RELOAD, location = "login.action", type = "redirect"),
	@Result(name = "login", location = "/WEB-INF/content/userinfo/login.jsp"),
	@Result(name = "failure", location = "/WEB-INF/content/userinfo/login.jsp")})
	
public class UserInfoAction extends ActionSupport{

	private static final long serialVersionUID = 7975422159606754668L;
	
	private User user;
	private String menus = "";

	@Autowired
	private UserInfoManager userinfoManager;

	public String login() {
		List<PropertyFilter> filters = HibernateWebUtils.buildPropertyFilters(Struts2Utils.getRequest());
		if (filters == null || filters.size() == 0)
			return "failure";
		List<User> users = userinfoManager.findUserByName(filters);
		if (users == null || users.size() == 0) {
			addActionMessage("无此用户或密码错误");
			return "failure";
		}
		User user = users.get(0);
		if (!user.isValid()) {
			addActionMessage("会员已注销，请重新开通");
			return "failure";
		}
		Struts2Utils.getSession().setAttribute(PolicyConstants.IDENTITY_MEMBER,
				users.get(0));
		return "login";
	}
	
	@Override
	public String execute() throws Exception {
		return login();
	}

	public String main() {
		List<PropertyFilter> filters = HibernateWebUtils
				.buildPropertyFilters(Struts2Utils.getRequest());

		if (filters.size() == 0) {
			addActionMessage("登录已失效，请重新登录");
			return "failure";
		}
		List<User> users = userinfoManager.findUserByName(filters);
		if (users == null || users.size() == 0) {
			addActionMessage("用户名密码不匹配");
			return "failure";
		}
		user = users.get(0);
		if (!user.isValid()) {
			addActionMessage("用户不存在");
			return "failure";
		}
		Struts2Utils.getSession().setAttribute(PolicyConstants.IDENTITY_USER,
				users.get(0));// 设置到session

		setMenus();

		return "main";
	}

	public void setMenus() {
		int i = 0;
		String title = "";
		for (Resource resource : user.getAllMenu()) {
			if (!resource.getModule().equals(title)) {
				if (i != 0) {
					menus += "</div>";
				}
				menus += "<h3 class=\"ui-accordion-header ui-helper-reset ui-state-active ui-corner-top\"><a href=\"#\">"
						+ resource.getModule()
						+ "</a></h3><div class=\"ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom\">";
				title = resource.getModule();
			}
			menus += "<a href=\"" + resource.getAction()
					+ "\" target=\"mainFrame\">" + resource.getName()
					+ "</a><br>";
			i++;
		}
		menus += "</div>";
	}

	public User getUser() {
		return user;
	}

	public String getMenus() {
		return menus;
	}
}
